"""
查找替换嵌套的 date_format 函数
"""

import logging
import re


def change_pre_month_first_day_unix_date(m):
    logging.info("change_pre_month_first_day_unix_date")
    logging.info(m)
    new_date_format = "TO_CHAR(CURRENT_DATE + INTERVAL '{} MONTH','yyyy-MM-01')".format(
        str(m.group(1).strip()).replace("'", ""))
    return new_date_format


def change_now_df_ymd_sub_day_df_ymd(m):
    logging.info("change_now_df_ymd_sub_day_df_ymd")
    logging.info(m)
    new_date_format = "TO_CHAR(CURRENT_DATE - INTERVAL '{} day','yyyyMMdd')".format(
        str(m.group(6).strip()).replace("'", ""))
    return new_date_format


def change_df_date_add_df_day_hyphen(m):
    logging.info("change_df_date_add_df_day_hyphen")
    logging.info(m)
    # DATE_FORMAT(date_add(NOW(), INTERVAL 1 day), '%Y-%m-%d')
    new_date_format = "TO_CHAR(CURRENT_DATE + INTERVAL '{} DAY','yyyy-MM-dd')".format(
        str(m.group(6).strip()).replace("'", ""))
    return new_date_format

def change_df_date_sub_now_day_hyphen(m):
    # date_format(date_sub(now(),interval 2 day),'%Y-%m-%d')
    logging.info("change_df_date_sub_now_day_hyphen")
    logging.info(m)
    # DATE_FORMAT(date_sub(NOW(), INTERVAL 1 day), '%Y-%m-%d')
    new_date_format = "TO_CHAR(CURRENT_DATE - INTERVAL '{} DAY','yyyy-MM-dd')".format(str(m.group(5).strip()).replace("'", ""))
    return new_date_format


def change_df_current_date_add_df_day_hyphen(m):
    logging.info("change_df_current_date_add_df_day_hyphen")
    logging.info(m)
    logging.info("change_df_current_date_add_df_day_hyphen")
    # DATE_FORMAT(date_add(NOW(), INTERVAL 1 day), '%Y-%m-%d')
    new_date_format = "TO_CHAR(CURRENT_DATE + INTERVAL '{} DAY','yyyy-MM-dd')".format(
        str(m.group(7).strip()).replace("'", ""))
    return new_date_format


def change_df_current_date_add_df_day_hyphen2(m):
    logging.info("change_df_current_date_add_df_day_hyphen2")
    logging.info(m)
    logging.info("change_df_current_date_add_df_day_hyphen2")
    new_date_format = "TO_CHAR(CURRENT_DATE + INTERVAL '{} DAY','yyyy-MM-dd')".format(
        str(m.group(5).strip()).replace("'", ""))
    return new_date_format



def change_now_df_ymd_add_day_df_ymd(m):
    logging.info("change_now_df_ymd_add_day_df_ymd")
    logging.info(m)
    new_date_format = "TO_CHAR(CURRENT_DATE + INTERVAL '{} DAY','yyyyMMdd')".format(
        str(m.group(6).strip()).replace("'", ""))
    return new_date_format


def change_next_day_df(m):
    logging.info("change_next_day_df")
    logging.info(m)
    logging.info("change_next_day_df")
    new_date_format = "TO_CHAR({} + INTERVAL '{} DAY','yyyyMMdd')".format(str(m.group(3).strip()).replace("'", ""),
                                                                          str(m.group(5).strip()).replace("'", ""))
    return new_date_format


def change_next_day_df_hyphen(m):
    logging.info("change_next_day_df_hyphen")
    logging.info(m)
    new_date_format = "TO_CHAR({} + INTERVAL '{} DAY','yyyy-MM-dd')".format(str(m.group(3).strip()).replace("'", ""),
                                                                            str(m.group(5).strip()).replace("'", ""))
    return new_date_format


def change_now_df_sub_df_ymd(m):
    logging.info("change_now_df_sub_df_ymd")
    logging.info(m)
    new_date_format = "TO_CHAR({} - INTERVAL '{} DAY','yyyyMMdd')" \
        .format(str(m.group(3).strip()).replace("'", ""), str(m.group(5).strip()).replace("'", ""))
    return new_date_format


def change_current_day_add_add_df_day_hyphen(m):
    logging.info("change_current_day_add_add_df_day_hyphen")
    logging.info(m)
    # DATE_FORMAT(     date_add(     date_add(NOW(), INTERVAL 1 day)    , INTERVAL -30 day)     , '%Y-%m-%d')
    # TO_CHAR(CURRENT_DATE + INTERVAL '1 day') - INTERVAL '30 day' ,'yyyy-MM-dd')
    new_date_format = "TO_CHAR((CURRENT_DATE + INTERVAL '{} DAY') + INTERVAL '{} DAY' ,'yyyy-MM-dd')" \
        .format(str(m.group(6).strip()).replace("'", ""), str(m.group(9).strip()).replace("'", ""))
    return new_date_format


def rep_complex_fn_date_format(document_content):
    """
    替换复杂嵌套的 date_format 函数
    :param document_content: 原始文本文件
    :return: 替换后的文本内容
    """
    logging.info("rep_complex_fn_date_format")

    df_patten = re.compile(
        r'concat\(date_format\(date_add\(NOW\(\)\,INTERVAL(.*)MONTH\)\,\'\%Y\%m\'\)\,\'01\'\)')
    result = df_patten.sub(change_pre_month_first_day_unix_date, document_content)
    document_content = result

    # DATE_FORMAT(date_add(NOW(),interval -30 day),'%Y-%m-%d')
    # date_format(date_add(date_format(NOW(),'%Y%m%d'),INTERVAL 1 day),'%Y%m%d')

    # # date_format(date_sub(date_format(NOW(), '%Y%m%d'), INTERVAL 10 day),'%Y%m%d')
    df_patten = re.compile(
        r"(DATE_FORMAT|date_format)\((DATE_SUB|date_sub)\((DATE_FORMAT|date_format)\((NOW|now)\(\)\,\'\%Y\%m\%d\'\)\,(INTERVAL|INTERVAL) (.*) (DAY|day)\)\,\'\%Y\%m\%d\'\)")
    result = df_patten.sub(change_now_df_ymd_sub_day_df_ymd, document_content)
    document_content = result

    # DATE_FORMAT\(date_add\(date_add\(NOW\(\)\, INTERVAL 1 day\)\, INTERVAL \-30 day\)\, \'\%Y\-\%m\-\%d\'\)
    df_patten = re.compile(
        r"(DATE_FORMAT|date_format)\((DATE_ADD|date_add)\((DATE_ADD|date_add)\((NOW|now)\(\)\,(INTERVAL|INTERVAL) (.*) (DAY|day)\)\,(INTERVAL|INTERVAL) (.*) (DAY|day)\)\,\'\%Y\-\%m\-\%d\'\)")
    result = df_patten.sub(change_current_day_add_add_df_day_hyphen, document_content)
    document_content = result

    # # date_format(date_add(date_format(now(), '%Y%m%d'), INTERVAL 10 day),'%Y%m%d')
    # date_format(date_add(date_format(now(), '%Y%m%d'), INTERVAL 1 day), '%Y%m%d')
    df_patten = re.compile(
        r"(DATE_FORMAT|date_format)\((DATE_ADD|date_add)\((DATE_FORMAT|date_format)\((NOW|now)\(\)\,\'\%Y\%m\%d\'\)\,(INTERVAL|INTERVAL)([^\(\)]*)(DAY|day)\)\,\'\%Y\%m\%d\'\)")
    result = df_patten.sub(change_now_df_ymd_add_day_df_ymd, document_content)
    document_content = result

    # DATE_FORMAT(date_add(NOW(), INTERVAL 1 day), '%Y-%m-%d')
    # DATE_FORMAT(date_add(NOW(), interval 1 day), '%Y-%m-%d')
    # DATE_FORMAT(date_add(NOW(), interval -30 day), '%Y-%m-%d')
    # DATE_FORMAT(date_add(NOW(), interval -30 day), '%Y-%m-%d')
    # print("-----------------------")
    # print(document_content)
    # print("-----------------------")
    # DATE_FORMAT(date_add(NOW(),interval -30 day),'%Y-%m-%d')
    # DATE_FORMAT\(date_add\(NOW\(\)\,interval \-30 day\)\,\'\%Y\-\%m\-\%d\'\)
    df_patten = re.compile(
        r"(DATE_FORMAT|date_format)\((DATE_ADD|date_add)\((NOW|now)\(\)\,(INTERVAL|INTERVAL) (.*) (DAY|day)\)\,\'\%Y\-\%m\-\%d\'\)")
    result = df_patten.sub(change_df_current_date_add_df_day_hyphen, document_content)
    document_content = result

    df_patten = re.compile(
        r"(DATE_FORMAT|date_format)\((date_add|DATE_ADD)\((NOW|now)\(\)\,(interval|INTERVAL)(.*)(day|DAY)\)\,\'\%Y\-\%m\-\%d\'\)")
    result = df_patten.sub(change_df_current_date_add_df_day_hyphen2, document_content)
    document_content = result

    # DATE_FORMAT(date_add(t1.dt, INTERVAL 1 DAY), '%Y%m%d')
    df_patten = re.compile(
        r'(DATE_FORMAT|date_format)\((DATE_ADD|date_add)\((.*),(INTERVAL|INTERVAL) (.*) (DAY|day)\),\'%Y%m%d\'\)')
    result = df_patten.sub(change_next_day_df, document_content)
    document_content = result

    df_patten = re.compile(
        r'(DATE_FORMAT|date_format)\((DATE_ADD|date_add)\((.*),(INTERVAL|INTERVAL) (.*) (DAY|day)\),\'%Y-%m-%d\'\)')
    result = df_patten.sub(change_df_date_add_df_day_hyphen, document_content)
    document_content = result

    # DATE_FORMAT(date_sub(t1.dt, INTERVAL 1 DAY), '%Y%m%d')
    df_patten = re.compile(
        r'(DATE_FORMAT|date_format)\((DATE_SUB|date_sub)\((.*),(INTERVAL|INTERVAL)([^\(\)]*)(DAY|day)\),\'%Y%m%d\'\)')
    result = df_patten.sub(change_now_df_sub_df_ymd, document_content)
    document_content = result

    # date_format(date_sub(now(),interval 2 DAY),'%Y-%m-%d')
    df_patten = re.compile(
        r"(DATE_FORMAT|date_format)\((DATE_SUB|date_sub)\((NOW|now)\(\)\,(INTERVAL|interval)(.*)(DAY|day)\)\,\'\%Y\-\%m\-\%d\'\)")
    result = df_patten.sub(change_df_date_sub_now_day_hyphen, document_content)
    document_content = result


    return document_content
